package com.prs.crm.action.assets;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.assets.AssetsBigClass;
import com.prs.crm.domain.assets.AssetsLingYong;
import com.prs.crm.domain.assets.AssetsSmallClass;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.assets.AssetsBigClassService;
import com.prs.crm.service.assets.AssetsSmallClassService;

public class AssetsSmallClassAction extends BaseAction{
	private PaginationSupport pagination;
	private Integer[] ids;
	private AssetsBigClassService assetsBigClassService;
	private AssetsSmallClassService assetsSmallClassService;
	private List<AssetsBigClass> bigClass;
	private AssetsSmallClass assetsSmallClass;
	private Application application;
	private ApplicationService ApplicationService;
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public AssetsBigClassService getAssetsBigClassService() {
		return assetsBigClassService;
	}
	public void setAssetsBigClassService(AssetsBigClassService assetsBigClassService) {
		this.assetsBigClassService = assetsBigClassService;
	}
	public AssetsSmallClassService getAssetsSmallClassService() {
		return assetsSmallClassService;
	}
	public void setAssetsSmallClassService(
			AssetsSmallClassService assetsSmallClassService) {
		this.assetsSmallClassService = assetsSmallClassService;
	}
	public AssetsSmallClass getAssetsSmallClass() {
		return assetsSmallClass;
	}
	public void setAssetsSmallClass(AssetsSmallClass assetsSmallClass) {
		this.assetsSmallClass = assetsSmallClass;
	}
	public List<AssetsBigClass> getBigClass() {
		return bigClass;
	}
	public void setBigClass(List<AssetsBigClass> bigClass) {
		this.bigClass = bigClass;
	}
	//////////////////////////////////////////////////////////////////////////////
	@Action("listSmallClass")
	public String listSmallClass(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(AssetsSmallClass.class);
		this.setPagination(this.assetsSmallClassService.listByPage(criteria, 
				this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("editSmallClass")
	public String editSmallClass(){
		bigClass = this.getAssetsBigClassService().getAll();
		return SUCCESS;
	}
	@Action(value="saveSmallClass",results={
			@Result(name="success",type="redirect",location="listSmallClass"),
			@Result(name="error",type="chain",location="editSmallClass")})
	public String saveSmallClass(){
		assetsSmallClass.setRecorder(null);
		assetsSmallClass.setRecordTime(null);
		assetsSmallClass.setSign(null);
		this.assetsSmallClassService.save(assetsSmallClass);
		application = new Application();
		application.setType("一般");
		application.setAppName("[一般]"+"资产分类");
		application.setSqType(2);
		application.setSpr(0);
		application.setPerson(assetsSmallClass.getRecorder());
		application.setResign("资产分类");
		application.setAssetsSmallClass(assetsSmallClass);
		this.getApplicationService().save(application);
		return SUCCESS;
	}
	//保存并继续
	@Action(value="saveedSmallClass",results={
			@Result(name="success",type="redirect",location="editSmallClass"),
			@Result(name="error",type="chain",location="editSmallClass")})
	public String saveedSmallClass(){
		assetsSmallClass.setRecorder(null);
		assetsSmallClass.setRecordTime(null);
		assetsSmallClass.setSign(null);
		this.assetsSmallClassService.save(assetsSmallClass);
		return SUCCESS;
	}
	@Action(value="removeSmallClass",results={
			@Result(name="success",type="redirect",location="listSmallClass"),
			@Result(name="error",type="chain",location="listSmallClass")})
	public String removeSmallClass(){
		if (assetsSmallClass.getApplication().size()>0) {
			addActionError("该类已被关联，不能删除！");
			return ERROR;
		}
		this.assetsSmallClassService.remove(assetsSmallClass);
		return SUCCESS;
	}
	@Action("viewSmallClass")
	public String viewSmallClass(){
		bigClass = this.getAssetsBigClassService().getAll();
		return SUCCESS;
	}
}
